﻿using Aliphant.Mvvm.XEvent.Events.Base;
using Aliphant.Mvvm.XEvent.Interface;

namespace Aliphant.Mvvm.XEvent.Instance
{
    public class EventManagerAgent : IEventManager
    {
        private readonly List<EventBase> _eventList = new List<EventBase>();
        protected ICollection<EventBase> EventList => _eventList;

        public T GetEvent<T>() where T : EventBase, new()
        {
            lock (EventList)
            {
                foreach (EventBase item in EventList)
                {
                    Type itemType = item.GetType();
                    if (itemType.Equals(typeof(T)))
                    {
                        return (T)item;
                    }
                }
                var obj = new T();
                EventList.Add(obj);
                return obj;
            }
        }

    }
}
